package com.khd.problems_0001_0100;

import java.util.HashMap;
import java.util.Map;

/**
 * 13. 罗马数字转整数
 * https://leetcode-cn.com/problems/roman-to-integer/
 *
 * @author kehandi
 * @title: Problem_13_RomanToInteger
 * @projectName algorithm
 * @date 2022/3/27 17:54
 */
public class Problem_0013_RomanToInteger {

    Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    public int romanToInt(String s) {
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int value = symbolValues.get(s.charAt(i));
            if (i < n - 1 && value < symbolValues.get(s.charAt(i + 1))) {
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Problem_0013_RomanToInteger problem_13_romanToInteger = new Problem_0013_RomanToInteger();
        System.out.println(problem_13_romanToInteger.romanToInt("III"));
        System.out.println(problem_13_romanToInteger.romanToInt("IV"));
        System.out.println(problem_13_romanToInteger.romanToInt("IX"));
        System.out.println(problem_13_romanToInteger.romanToInt("LVIII"));
        System.out.println(problem_13_romanToInteger.romanToInt("MCMXCIV"));

    }
}
